<?php
/**
 * Created by Veyron
 * User: Vy
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/1/17
 * Time: 11:33
 */
declare(strict_types=1);
namespace app\api\controller;
use app\api\validate\User as UserValidate;
use app\common\business\User as UserBus;

class Login extends ApiBase {
    public function index():object {
        if(!$this->request->isPost()){
            return show(config('status.error'),'非法请求');
        }
        $phone_number = $this->request->param('phone_number','','trim');
        $code         = $this->request->param('code','','intval');
        $type         = $this->request->param('type','','intval');
        //参数效验
        $data = [
            'phone_number' =>$phone_number,
            'code'        => $code,
            'type'        => $type,
        ];
        $vliadate = new UserValidate();
        if(!$vliadate->scene('login')->check($data)){
            return show(config('status.error'),$vliadate->getError());
        }
        try {
            $userBus = new UserBus();
            $user = $userBus->login($data);
        }catch (\Exception $e){
            return show($e->getCode(),$e->getMessage());
        }

        if($user){
            return show(config('status.success'),'登录成功',$user);
        }
        return show(config('status.error'),'发送验证码失败');
    }

}
